package net.arogarth.aws.android.manager.views;

import java.util.ArrayList;
import java.util.List;

import net.arogarth.aws.android.manager.AwsManager;
import net.arogarth.aws.android.manager.R;
import net.arogarth.aws.android.manager.dbmodels.AwsAccount;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class AwsAccountList extends Activity {
	public class AwsAccountListAdapter extends BaseAdapter implements
			AdapterView.OnItemClickListener,
			AdapterView.OnItemLongClickListener {

		private Context context;
		public List<AwsAccount> accounts = new ArrayList<AwsAccount>();

		public AwsAccountListAdapter(Context context, ListView list) {
			this.context = context;

			list.setEmptyView(findViewById(R.id.btnEmpty));
			list.setAdapter(this);
			list.setOnItemClickListener(this);
			list.setOnItemLongClickListener(this);
		}

		public int getCount() {
			return this.accounts.size();
		}

		public AwsAccount getItem(int position) {
			return this.accounts.get(position);
		}

		public long getItemId(int position) {
			return this.accounts.get(position).getId();
		}

		public View getView(int arg0, View arg1, ViewGroup parent) {
			final LinearLayout _ll = (LinearLayout) LayoutInflater.from(
					this.context).inflate(R.layout.aws_account_list_item,
					parent, false);

			final AwsAccount account = this.getItem(arg0);

			((TextView) _ll.findViewById(R.id.accountName)).setText(account
					.getName());

			return _ll;
		}

		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			AwsManager.setCurrentAwsAccount(this.getItem(position));

			startActivity(new Intent(AwsAccountList.this, Main.class));
		}

		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int position, long arg3) {
			final AwsAccount account = this.getItem(position);
			AlertDialog.Builder builder = new AlertDialog.Builder(this.context);

			builder.setTitle(account.getName());

			ArrayAdapter<CharSequence> adapter = ArrayAdapter
					.createFromResource(this.context, R.array.awsAccountptions,
							android.R.layout.select_dialog_item);

			builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
				@SuppressWarnings("unchecked")
				public void onClick(DialogInterface dialog, final int which) {

					switch (which) {
					case 0: // DELETE
						account.delete();

						accounts = (List<AwsAccount>) account.listAll();

						notifyDataSetChanged();
						break;
					}
				}
			});

			builder.create().show();

			return true;
		}
	}

	private AwsAccountListAdapter la;

	@SuppressWarnings("unchecked")
	@Override
	public void onResume() {
		super.onResume();
		la.accounts = (List<AwsAccount>) new AwsAccount().listAll();
		la.notifyDataSetChanged();
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.aws_account_list);
		final ListView lv = (ListView) findViewById(R.id.contentlist);

		la = new AwsAccountListAdapter(AwsAccountList.this, lv);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.manage_account, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case R.id.miAddAccount:
			startAddAccountActivity(null);
			return true;
		case R.id.miImport:
			startActivity(new Intent(AwsAccountList.this, ImportCsv.class));
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void startAddAccountActivity(View call) {
		startActivity(new Intent(AwsAccountList.this, CreateNewAwsAccount.class));
	}
}
